package com.zx.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.reggie.common.CustomException;
import com.zx.reggie.mapper.DishMapper;
import com.zx.reggie.pojo.Dish;
import com.zx.reggie.pojo.DishDto;
import com.zx.reggie.pojo.DishFlavor;
import com.zx.reggie.service.DishFlavorService;
import com.zx.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 张旭
 * @version 1.0
 */

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //调用原来的save方法
        this.save(dishDto);

        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public DishDto getBack(long id) {

        //查Dish数据表
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        //数据赋值
        BeanUtils.copyProperties(dish, dishDto);

        //查dish_flavor表
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //传输的是dish的子类，查dish表
        this.updateById(dishDto);

        //先将dish_flavor表中的相关数据删除
        //这里并不能直接更新数据，因为update语句每次执行只能影响一条，所以需要先删除再插入
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getId, dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);

        //再在dish_flavor中插入数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
        return;
    }

    @Override
    @Transactional
    public void removeWithFlavor(List<Long> ids) {
        //不仅需要删除该菜品，也要删除该菜品的口味


        //判断id在ids中的，以及状态为在售的数据条数count
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId,ids);
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        int count = this.count(lambdaQueryWrapper);

        if(count > 0){
            throw new CustomException("菜品正在销售，不能删除");
        }
        //count不大于0,可以删除
        this.removeByIds(ids);

        //删除跟该菜品关联的菜品口味表中的数据
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);


    }
}
